<?php

namespace Apipad\Controller;

use Think\Controller;

//企业
class CompanyController extends CommonController
{

    //列表
    public function index()
    {
        $model = M('company');
        $detail = I('post.');
        $page = $detail['page'] ? $detail['page'] : 1;
        $limit = $detail['limit'] ? $detail['limit'] : 15;
        $kewords = $detail['kewords'];
        $uid = UID;
        $where['status'] = 1;
        $where['aId'] = $uid;
        if ($kewords) {
            $where['company_sn|companyName|contact'] = ['like', '%' . $kewords . '%'];
        }
        $count = $model->where($where)->count();
        $list = $model
            ->where($where)
            ->order('id desc')->page($page, $limit)->select();
        $data_cc['code'] = 200;
        $data_cc['msg'] = '读取成功';
        $data_cc['data'] = $list;
        $data_cc['count'] = $count;
        exit(json_encode($data_cc));
    }

    public function add()
    {
        $detail = I('post.');
        $model = M('company');
        if (empty($detail['companyName'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '企业名称不能为空';
            exit(json_encode($data_cc));
        }
        if (empty($detail['contact'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '联系人不能为空';
            exit(json_encode($data_cc));
        }
        if (empty($detail['mobile'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '联系号码不能为空';
            exit(json_encode($data_cc));
        }
        $s_data['updatetime'] = time();
        $s_data['createtime'] = time();
        $s_data['companyName'] = $detail['companyName'];
        $s_data['contact'] = $detail['contact'];
        $s_data['mobile'] = $detail['mobile'];
        $s_data['aId'] = UID;
        $result = $model->add($s_data);
        if ($result) {
            $company_sn = 'sj' . $result;
            $model->where(['id' => $result])->save(['company_sn' => $company_sn]);
            $data_cc['code'] = 200;
            $data_cc['msg'] = '添加成功';
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '添加失败';
            exit(json_encode($data_cc));
        }
    }

    public function edit()
    {
        $detail = I('post.');
        $model = M('company');
        $id = $detail['id'];
        if (empty($detail['companyName'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '企业名称不能为空';
            exit(json_encode($data_cc));
        }
        if (empty($detail['contact'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '联系人不能为空';
            exit(json_encode($data_cc));
        }
        if (empty($detail['mobile'])) {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '联系号码不能为空';
            exit(json_encode($data_cc));
        }
        $s_data['updatetime'] = time();
        $s_data['companyName'] = $detail['companyName'];
        $s_data['contact'] = $detail['contact'];
        $s_data['mobile'] = $detail['mobile'];
        $result = $model->where(['id' => $id])->save($s_data);
        if ($result) {
            $company_sn = 'sj' . $result;
            $model->where(['id' => $result])->save(['company_sn' => $company_sn]);
            $data_cc['code'] = 200;
            $data_cc['msg'] = '添加成功';
            exit(json_encode($data_cc));
        } else {
            $data_cc['code'] = 0;
            $data_cc['msg'] = '添加失败';
            exit(json_encode($data_cc));
        }
    }

    public function del()
    {
        $detail = I('post.');
        $id = $detail['id'];
        if (empty($id)) {
            $data['code'] = 0;
            $data['msg'] = 'ID不能为空';
            exit(json_encode($data));
        }
        $model = M('company');
        $info = $model->where(['id' => $id])->find();
        if (!$info) {
            $data['code'] = 0;
            $data['msg'] = '未查到该对应的数据';
            exit(json_encode($data));
        }
        $map['id'] = $id;
        $s_data['status'] = 0;
        $s_data['deletetime'] = time();
        $result = $model->where($map)->save($s_data);
        if ($result) {
            $data['code'] = 200;
            $data['msg'] = '删除成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '删除失败';
            exit(json_encode($data));
        }
    }

    public function detail()
    {
        $detail = I('post.');
        $id = $detail['id'];
        if (empty($id)) {
            $data['code'] = 0;
            $data['msg'] = 'ID不能为空';
            exit(json_encode($data));
        }
        $model = M('company');
        $info = $model->where(['id' => $id])->find();
        if (!$info) {
            $data['code'] = 0;
            $data['msg'] = '未查到该对应的数据';
            exit(json_encode($data));
        }
        $data['code'] = 200;
        $data['msg'] = '查找成功';
        $data['data'] = $info;
        exit(json_encode($data));
    }

    public function addCategory()
    {
        $detail = I('post.', '', 'strip_tags');
        $model = M('sale_category');
        $detail['domains'] = json_decode($detail['domains'], true);
        $detail['fieldList'] = json_encode($detail['domains']);
        $data = [
            'company_id' => $detail['company_id'],
            'createtime' => time(),
            'updatetime' => time(),
            'aId' => UID,
            'fieldList' => $detail['fieldList'],
            'category_name' => $detail['categoryname'],
        ];
        if ($model->where(['category_name' => $detail['categoryname']])->find()) {
            $data['code'] = 0;
            $data['msg'] = '已存在相同分类的字段了！';
            exit(json_encode($data));
        }
        $res = $model->add($data);
        if ($res) {
            $data['code'] = 200;
            $data['msg'] = '新增成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 200;
            $data['msg'] = '新增失败';
            exit(json_encode($data));
        }
    }


    public function editCategory()
    {
        $detail = I('post.', '', 'strip_tags');
        $model = M('sale_category');
        $detail['domains'] = json_decode($detail['domains'], true);
        $detail['fieldList'] = json_encode($detail['domains']);
        $data = [
            'updatetime' => time(),
            'fieldList' => $detail['fieldList'],
            'category_name' => $detail['categoryname'],
        ];
        if ($model->where(['category_name' => $detail['categoryname']])->where(['id' => $detail['id']])->find()) {
            $data['code'] = 0;
            $data['msg'] = '已存在相同分类的字段了！';
            exit(json_encode($data));
        }
        $res = $model->where(['id' => $detail['id']])->save($data);
        if ($res) {
            $data['code'] = 200;
            $data['msg'] = '修改成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 200;
            $data['msg'] = '修改失败';
            exit(json_encode($data));
        }
    }

    public function mysqls_cate()
    {
        $model = M('sale_category');
        $map = [];
        $detail = I('post.');
        $map['pid'] = 0;
        $map['status'] = 1;
        $map['company_id'] = $detail['company_id'];
        $list = $model->where($map)->order('id desc')->select();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data']['list'] = $list;
        exit(json_encode($data));
    }

    public function getFiledList()
    {
        $model = M('sale_category');
        $detail = I('post.');
        $datas = $model->where(['id' => $detail['id']])->order('id desc')->find();
        $list = json_decode($datas['fieldList'], true);
        $datas['fieldList'] = $list;
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data'] = $datas;
        exit(json_encode($data));

    }

    public function getDataList()
    {
        $model = M('company_standard_list');
        $detail = I('post.');
        $page = $detail['page'] ? $detail['page'] : 1;
        $limit = $detail['limit'] ? $detail['limit'] : 15;
        $where = [];
        if (!empty($detail['keyword'])) {
            $keyword = $detail['keyword'];
            $where['size'] = array('like', '%' . $keyword . '%');

        }
        $list = $model->where($where)->where(['company_id' => $detail['company_id']])->where(['category_id' => $detail['category_id']])->order('id desc')->page($page, $limit)->select();
        foreach ($list as $k => $v) {
            $list[$k]['fieldList'] = json_decode($list[$k]['fieldList'], true);
            $list[$k]['fieldList']['id'] = $list[$k]['id'];
            $list[$k] = $list[$k]['fieldList'];
        }
        $count = $model->where(['company_id' => $detail['company_id']])->where(['category_id' => $detail['category_id']])->count();
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data'] = $list;
        $data['count'] = $count;
        exit(json_encode($data));
    }

    public function getCategoryDetail()
    {
        $detail = I('post.');
        $model = M('sale_category');
        $data = $model->where(['id' => $detail['id']])->find();
        $data['fieldList'] = json_decode($data['fieldList'], true);
        $data['code'] = 200;
        $data['msg'] = '查询成功';
        $data['data'] = $data;
        exit(json_encode($data));
    }


    public function addStandard()
    {
        $model = M('company_standard_list');
        $detail = I('post.', '', 'strip_tags');
        $detail['fieldList'] = json_decode($detail['fieldList'], true);
        $data = [
            'updatetime' => time(),
            'createtime' => time(),
            'aId' => UID,
            'company_id' => $detail['company_id'],
            'category_id' => $detail['category_id'],
            'size' => $detail['fieldList']['size'],
            'height' => $detail['fieldList']['high'],
            'weight' => $detail['fieldList']['weight'],
            'fieldList' => json_encode($detail['fieldList'])
        ];
        $res = $model->add($data);
        if ($res) {
            $data['code'] = 200;
            $data['msg'] = '新增成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '新增失败';
            exit(json_encode($data));
        }
    }

    public function getStandardDetail()
    {
        $model = M('company_standard_list');
        $detail = I('post.');
        $res = $model->where(['id' => $detail['id']])->find();
        $res['high'] = $res['height'];
        $res['fieldList'] = json_decode($res['fieldList'], true);
        if ($res) {
            $data['code'] = 200;
            $data['msg'] = '查找成功';
            $data['data'] = $res['fieldList'];
            exit(json_encode($data));
        }
    }

    public function editStandard()
    {
        $model = M('company_standard_list');
        $detail = I('post.', '', 'strip_tags');
        $detail['fieldList'] = json_decode($detail['fieldList'], true);
        $data = [
            'updatetime' => time(),
            'createtime' => time(),
            'aId' => UID,
            'company_id' => $detail['company_id'],
            'category_id' => $detail['category_id'],
            'size' => $detail['fieldList']['size'],
            'height' => $detail['fieldList']['high'],
            'weight' => $detail['fieldList']['weight'],
            'fieldList' => json_encode($detail['fieldList'])
        ];
        $res = $model->where(['id' => $detail['id']])->save($data);
        if ($res) {
            $data['code'] = 200;
            $data['msg'] = '修改成功';
            exit(json_encode($data));
        } else {
            $data['code'] = 0;
            $data['msg'] = '修改失败';
            exit(json_encode($data));
        }
    }

    public function delStandard()
    {
        $model = M('company_standard_list');
        $detail = I('post.');
        $res = $model->where(['id' => $detail['id']])->save(['updatetime' => time(), 'status' => 0]);
        if ($res) {
            $data['code'] = 200;
            $data['msg'] = '删除成功';
            $data['data'] = $res;
            exit(json_encode($data));
        }

    }

}